package com.ruoyi.marketing.service;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.marketing.client.CreativityClient;
import com.ruoyi.marketing.controller.MarKetingAuth2Controller;
import com.ruoyi.marketing.pojo.bo.CreativityBo;
import com.ruoyi.marketing.pojo.vo.AuthVo;
import com.ruoyi.marketing.pojo.vo.CreativityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @description: 
 *          ________
 *          ___  __/______________
 *          __  /_ ___  __ \  ___/
 *          _  __/ __  /_/ / /__
 *          /_/    _  .___/\___/
 *                 /_/
 * @author: www.fpcs.top
 *
 * @create: 2021-05-27 15:33
 */
@Service
public class CreativityService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CreativityClient creativityClient;

    public List getCreativityStatistics(CreativityBo creativityBo) {
        Set keys = redisTemplate.keys(MarKetingAuth2Controller.CACHE_Auth2_KEY + "*");

        List list1=new ArrayList();
        keys.forEach(key->{
            String user = JSONUtils.toJSONString(redisTemplate.opsForValue().get(key));
            AuthVo authVo = JSONUtil.toBean(user, AuthVo.class);
            String s = authVo.getAdvertiserProId().get(0).toString();
            HashMap hashMap = JSONUtil.toBean(s, HashMap.class);
            creativityBo.setAdvertiser_id(Long.parseLong(hashMap.get("advertiser_id").toString()));
            CreativityVo creativityData = creativityClient.getCreativityData(creativityBo,authVo.getAccessToken());
            list1.add(creativityData.getData());
        });
        return list1;
    }
}